package org.lemon.transport.mqtt.server.session;

import io.netty.handler.codec.mqtt.MqttQoS;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.lemon.transport.context.auth.TransportDeviceInfo;
import org.lemon.transport.context.data.device.DeviceProfile;
import org.lemon.transport.context.service.TransportService;
import org.lemon.transport.context.session.SessionMsgListener;
import org.lemon.transport.mqtt.server.handler.GatewaySessionHandler;
import org.lemon.transport.mqtt.server.model.MqttTopicMatcher;
import org.server.transport.gen.SessionProto;
import org.server.transport.gen.TransportProto;

import java.util.UUID;
import java.util.concurrent.ConcurrentMap;

@Slf4j
public class GatewayDeviceSessionContext extends MqttDeviceAwareSessionContext implements SessionMsgListener {

    private final GatewaySessionHandler handler;
    private final TransportService transportService;

    public GatewayDeviceSessionContext(GatewaySessionHandler handler, TransportDeviceInfo deviceInfo,
                                       DeviceProfile deviceProfile, ConcurrentMap<MqttTopicMatcher, Integer> mqttQoSMap,
                                       TransportService transportService) {
        super(UUID.randomUUID(), mqttQoSMap);
        this.handler = handler;
        this.transportService = transportService;
        SessionProto.SessionInfoProto sessionInfo = SessionProto.SessionInfoProto.newBuilder()
                .setNodeId(handler.getNodeId())
                .setSessionIdMSB(sessionId.getMostSignificantBits())
                .setSessionIdLSB(sessionId.getLeastSignificantBits())
                .setTenantId(deviceInfo.getTenantId().getValue())
                .setDeviceId(deviceInfo.getDeviceId())
                .setProjectId(deviceInfo.getProjectId())
                .setDeviceName(deviceInfo.getDeviceName())
                .setDeviceType(deviceInfo.getDeviceType())
                .setGwSessionIdMSB(handler.getSessionId().getMostSignificantBits())
                .setGwSessionIdLSB(handler.getSessionId().getLeastSignificantBits())
                .setDeviceProfileId(deviceInfo.getDeviceProfileId())
                .build();
        setSessionInfo(sessionInfo);
        setDeviceInfo(deviceInfo);
        setConnected(true);
        setDeviceProfile(deviceProfile);
    }

    @Override
    public UUID getSessionId() {
        return sessionId;
    }

    @Override
    public int nextMsgId() {
        return handler.nextMsgId();
    }

    @Override
    public void onRemoteSessionCloseCommand(UUID sessionId, TransportProto.SessionCloseNotificationProto sessionCloseNotification) {

    }

    @Override
    public void onGetAttributesResponse(TransportProto.GetAttributeResponseMsg attributesResponse) {

    }

    @Override
    public void onAttributeUpdate(UUID sessionId, TransportProto.AttributeUpdateNotificationMsg attributeUpdateNotification) {

    }

    @Override
    public void onDeviceDeleted(Long deviceId) {

    }

    @Override
    public void onToDeviceRpcRequest(UUID sessionId, TransportProto.ToDeviceRpcRequestMsg toDeviceRequest) {

    }

    @Override
    public void onToServerRpcResponse(TransportProto.ToServerRpcResponseMsg toServerResponse) {

    }

    @Override
    public void onToTransportUpdateCredentials(TransportProto.ToTransportUpdateCredentialsProto toTransportUpdateCredentials) {

    }
}
